//
//  CWSportGPSSignalButton.m
//  大神专制黑马行
//
//  Created by chenWei on 2017/5/15.
//  Copyright © 2017年 陈伟. All rights reserved.
//

#import "CWSportGPSSignalButton.h"

@implementation CWSportGPSSignalButton

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    //注册监听 GPS 信号变化
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sportGpsSignalState:) name:kGPSSignalChangeNotification object:nil];
    
}

//移除通知
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kGPSSignalChangeNotification object:nil];
}

#pragma mark - 监听信号变化
- (void)sportGpsSignalState:(NSNotification *)info
{
    //1.获取传过来的信号状态
    HMSportGpsSignalState state = [info.userInfo[@"key"] integerValue];
    NSString *imageStr;
    NSString *titleStr;
    
    
    //NSLog(@"%@",self.nextResponder);//按钮的下一个响应者是视图
    //NSLog(@"%@",self.nextResponder.nextResponder);//视图的下一个响应者是自身所在的控制器
    //通过响应者连获取按钮所在的控制器
    UIViewController *vc = (UIViewController *)self.nextResponder.nextResponder;
    if([vc isKindOfClass:[NSClassFromString((@"CWSportingVC")) class]])
    {
        imageStr = @"ic_sport_gps_connect";
    }
    else if ([vc isKindOfClass:[NSClassFromString((@"CWSportMapVC")) class]])
    {
        imageStr = @"ic_sport_gps_map_connect";
    }

    
    //根据状态做出提示
    if (state == HMSportGpsSignalStateClose) {
        titleStr = @"GPS已断开";
    }
    else if (state == HMSportGpsSignalStateBad)
    {
        titleStr = @"建议绕开高楼大厦";
    }
    else
    {
        titleStr = nil;
    }
    
    //设置状态显示图片
    [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@_%zd",imageStr,state-1]] forState:UIControlStateNormal];
    [self setTitle:titleStr forState:UIControlStateNormal];
    
    //让按钮提前计算
    [self layoutIfNeeded];
    
    //刷新按钮的动画
    self.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1;
    }];
    
}


@end
